/*
 * This file is part of aion-engine <aion-engine.com>
 *
 * aion-engine is private software: you can redistribute it and or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Private Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.services.toypet;

import com.aionengine.gameserver.model.gameobjects.Pet;
import com.aionengine.gameserver.network.aion.serverpackets.SM_PET;
import com.aionengine.gameserver.network.aion.serverpackets.SM_SYSTEM_MESSAGE;
import com.aionengine.gameserver.services.item.ItemService;
import com.aionengine.gameserver.utils.PacketSendUtility;
import com.aionengine.gameserver.utils.audit.AuditLogger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author ATracer
 */
public class PetMoodService {

    private static final Logger log = LoggerFactory.getLogger(PetMoodService.class);

    public static void checkMood(Pet pet, int type, int shuggleEmotion) {
        switch (type) {
            case 0:
                startCheckingMood(pet);
                break;
            case 1:
                interactWithPet(pet, shuggleEmotion);
                break;
            case 3:
                requestPresent(pet);
                break;
        }
    }

    /**
     * @param pet
     */
    private static void requestPresent(Pet pet) {
        if (pet.getCommonData().getMoodPoints(false) < 9000) {
            log.warn("Requested present before mood fill up: {}", pet.getMaster().getName());
            return;
        }

        if (pet.getCommonData().getGiftRemainingTime() > 0) {
            AuditLogger.info(pet.getMaster(), "Trying to get gift during CD for pet " + pet.getPetId());
            return;
        }

        if (pet.getMaster().getInventory().isFull()) {
            PacketSendUtility.sendPacket(pet.getMaster(), SM_SYSTEM_MESSAGE.STR_WAREHOUSE_FULL_INVENTORY);
            return;
        }

        pet.getCommonData().clearMoodStatistics();
        PacketSendUtility.sendPacket(pet.getMaster(), new SM_PET(pet, 4, 0));
        PacketSendUtility.sendPacket(pet.getMaster(), new SM_PET(pet, 3, 0));
        int itemId = pet.getPetTemplate().getConditionReward();
        if (itemId != 0) {
            ItemService.addItem(pet.getMaster(), pet.getPetTemplate().getConditionReward(), 1);
        }
    }

    /**
     * @param pet
     * @param shuggleEmotion
     */
    private static void interactWithPet(Pet pet, int shuggleEmotion) {
        if (pet.getCommonData() != null) {
            if (pet.getCommonData().increaseShuggleCounter()) {
                PacketSendUtility.sendPacket(pet.getMaster(), new SM_PET(pet, 2, shuggleEmotion));
                PacketSendUtility.sendPacket(pet.getMaster(), new SM_PET(pet, 4, 0)); // Update progress immediately
            }
        }
    }

    /**
     * @param pet
     */
    private static void startCheckingMood(Pet pet) {
        PacketSendUtility.sendPacket(pet.getMaster(), new SM_PET(pet, 0, 0));
    }

}